<?php
/*
 module:		消息表
 create_time:	2024-03-13 09:46:04
 author:		
 contact:		
*/

namespace app\api\controller;

use app\api\model\MembersNewMessage as MembersNewMessageModel;
use app\api\service\MembersNewMessageService;

class MembersNewMessage extends Common
{


    /*start*/
    /**
     * @api {get} /MembersNewMessage/index 01、首页数据列表
     */
    function index()
    {
        $limit = $this->request->get('limit', 20, 'intval');
        $page = $this->request->get('page', 1, 'intval');

        $where = [];
//        $where['status'] = $this->request->get('status', '', 'serach_in');
//        $addtime_start = $this->request->get('addtime_start', '', 'serach_in');
//        $addtime_end = $this->request->get('addtime_end', '', 'serach_in');
//
//        $where['addtime'] = ['between', [strtotime($addtime_start), strtotime($addtime_end)]];
        $where['uid'] = $this->request->uid;
        $field = 'message_id,type,jobs_apply_id,status,addtime,is_see,content';
        $orderby = 'message_id desc';
        $res = MembersNewMessageService::indexList(formatWhere($where), $field, $orderby, $limit, $page);
        if (!empty($res["list"])) {
            $list = $res["list"];
            foreach ($list as &$item) {
                $item["addtime"] = timeAnalysis($item["addtime"]);

                if (!empty($item["content"])) {
                    $item["content"] = htmlspecialchars_decode($item["content"]);
                }
            }
            $res["list"] = $list;
        }
        return $this->ajaxReturn($this->successCode, '返回成功', htmlOutList($res));
    }

    /**
     * @api {post} /MembersNewMessage/add 02、添加
     */
    function add()
    {
        $postField = 'uid,type,jobs_apply_id,status,content,addtime';
        $data = $this->request->only(explode(',', $postField), 'post', null);
        $res = MembersNewMessageService::add($data);
        return $this->ajaxReturn($this->successCode, '操作成功', $res);
    }

    /**
     * @api {get} /MembersNewMessage/noSeeNum 03、未读信息数量
     */
    function noSeeNum()
    {
        $where = [];
        $where['uid'] = $this->request->uid;
        $where['is_see'] = 1;
        $res = MembersNewMessageModel::where($where)->count();
        return $this->ajaxReturn($this->successCode, '返回成功', ["count" => $res]);
    }

    /**
     * @api {get} /MembersNewMessage/view 04、查看详情
     */
    function view()
    {
        $data['message_id'] = $this->request->get('message_id', '', 'serach_in');
        $field = 'message_id,uid,type,jobs_apply_id,status,content,is_see,addtime';
        $item = MembersNewMessageModel::field($field)->where($data)->findOrEmpty()->toArray();
        $item["addtime"] = timeAnalysis($item["addtime"]);
        if (!empty($item["content"])) {
            $item["content"] = htmlspecialchars_decode($item["content"]);
        }
        if (!empty($item["is_see"]) && $item["is_see"] == 1) {
            MembersNewMessageModel::where($data)->update(["is_see" => 2]);
        }
        $item["jobs"] = ["id" => 0];
        if (!empty($item["type"]) && !empty($item["jobs_apply_id"]) && $item["type"] == 1) {
            $jobs_id = \app\api\model\Personaljobsapply::where("did", $item["jobs_apply_id"])->value("jobs_id");
            $fieldJods = 'id,jobs_name,companyname,company_id,nature_cn,sex_cn,age,topclass,category,subclass,category_cn,minwage,maxwage,trade_cn,district_cn,education_cn,experience_cn,tag_cn,refreshtime,click';
            $jobs = \app\api\model\Jobs::field($fieldJods)->where("id", $jobs_id)->findOrEmpty()->toArray();
            if (!empty($jobs)) {
                $jobs["refreshtime"] = timeAnalysis2($jobs["refreshtime"]);
                if (empty($jobs["tag_cn"]) || $jobs["tag_cn"] == "请选择") {
                    $jobs["tag_cn"] = [];
                } else {
                    $jobs["tag_cn"] = explode(",", $jobs["tag_cn"]);
                }
                $item["jobs"] = $jobs;
            }
        }
        return $this->ajaxReturn($this->successCode, '返回成功', $item);
    }
    /*end*/


}

